const formatTime = (dt, d = 0) => {
  var date = new Date(dt);
  date = new Date(date.setDate(date.getDate() + d));
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  //const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatDate = function(str_date, week = 0) {
  var date = new Date(str_date);
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var result = formatNumber(month) + '月' + formatNumber(day) + '日 ' + formatNumber(hour) + ':' + formatNumber(minute);
  if (week > 0) {
    result = formatNumber(month) + '月' + formatNumber(day) + '日(' + getWeekText(date.getDay()) + ') ' + formatNumber(hour) + ':' + formatNumber(minute);
  }
  return result;
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 获取周几
 * @param {Number} day 周X
 * @param {Number} p 显示格式,1:'星期',其他:'周';默认'周'
 */
const getWeekText = (day, p = 0) => {
  var prefix = p === 1 ? '星期' : '周';
  var week = '未知';
  switch (day) {
    case 0:
      week = prefix + '日';
      break;
    case 1:
      week = prefix + '一';
      break;
    case 2:
      week = prefix + '二';
      break;
    case 3:
      week = prefix + '三';
      break;
    case 4:
      week = prefix + '四';
      break;
    case 5:
      week = prefix + '五';
      break;
    case 6:
      week = prefix + '六';
      break;
  }
  return week;
}

const splitString = function(str, c = '|') {
  return str.split(c);
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatNumber: formatNumber,
  getWeekText: getWeekText,
  splitString: splitString
}